package tools;

import java.awt.Color;
import java.awt.geom.Ellipse2D;

import tools.vetorial.VectorialSelectionTool;
import core.DrawContext;
import core.Object2D;
import core.OurGraphics;
import core.VectorialObject;

public class RotateTool extends VectorialSelectionTool {

	private DrawContext mDrawContext;
	
	private int mRotateX = 0, mRotateY = 0;
	
	private ProxyRotate mProxy = new ProxyRotate();
	
	@Override
	public void init(DrawContext ctx) {
		super.init(ctx);
		mDrawContext = ctx;
		mDrawContext.addAnimatedObject2d(mProxy);
		mDrawContext.setAnimationChanged(true);
	}
	@Override
	public void dispose(DrawContext ctx) {
		// TODO Auto-generated method stub
		super.dispose(ctx);
		ctx.removeAnimatedObject2d(mProxy);
		mDrawContext.setAnimationChanged(true);
	}

	public void rotate(int x, int y, int teta) {
	
		for(VectorialObject obj : mDrawContext.getSelection()){
			obj.rotate(x, y, teta);
			mDrawContext.removeObject2d(obj);
			mDrawContext.addObject2d(obj);
		}
		
		mDrawContext.setChanged(true);
	}

	public void updateRotationCenter(int x, int y) {
		mRotateX = x;
		mRotateY = y;
		if(mDrawContext != null) {
			mDrawContext.setAnimationChanged(true);
		}
	}

	@Override
	public void onSelection(VectorialObject vo) {
		super.onSelection(vo);
	}
	
	private class ProxyRotate implements Object2D {

		private Ellipse2D.Double ellipse = new Ellipse2D.Double();
		
		@Override
		public void draw(OurGraphics g2) {
		
			ellipse.x = mRotateX - 4;
			ellipse.y = mRotateY - 4;
			ellipse.height = ellipse.width = 8;
			
			g2.getRealGraphics().setColor(Color.BLACK);
			g2.getRealGraphics().draw(ellipse);
		}
		
	}
}
